package main

import (
	"context"
	"fmt"
	"os"

	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/primitive"
	"github.com/apache/rocketmq-client-go/v2/producer"
)

func main() {
	p, _ := rocketmq.NewProducer(producer.WithNameServer([]string{"120.24.67.46:9876"}))
	err := p.Start()

	if err != nil {
		fmt.Printf("生成producer失败: %s", err.Error())
		os.Exit(1)
	}
	topic := "test"

	msg := &primitive.Message{
		Topic: topic,
		Body:  []byte("这是一条延迟消息"),
	}
	// 只需要加一个延迟级别即可
	msg.WithDelayTimeLevel(3)
	res, err := p.SendSync(context.Background(), msg)

	if err != nil {
		fmt.Printf("send message error: %s\n", err)
	} else {
		fmt.Printf("send message success: result=%s\n", res.String())
	}

	err = p.Shutdown()
	if err != nil {
		fmt.Printf("shutdown producer error: %s", err.Error())
	}
}
